<template>
  <el-card class="photo-config-item">
    <div slot="header">影楼用户信息</div>
    <el-form
      ref="form"
      :model="user"
      :rules="formRules"
      label-width="180px"
      status-icon
    >
      <el-form-item label="账号" prop="account">
        <el-input
          style="width: 220px"
          v-model.trim="user.account"
          placeholder="请输入账号"
          :disabled="id > 0"
          maxlength="20"
          show-word-limit
        ></el-input>
      </el-form-item>
      <el-form-item label="名称" prop="name">
        <el-input
          style="width: 220px"
          v-model.trim="user.name"
          placeholder="请输入名称"
          maxlength="10"
          show-word-limit
        ></el-input>
      </el-form-item>
      <el-form-item v-if="!id" label="密码" prop="password">
        <el-input
          style="width: 220px"
          type="password"
          placeholder="请输入密码"
          v-model.trim="user.password"
          auto-complete="new-password"
          minlength="6"
          show-word-limit
          maxlength="20"
        ></el-input>
      </el-form-item>
      <el-form-item label="是否启用" prop="enable">
        <el-select
          style="width: 150px"
          class="filter-item"
          placeholder="是否启用"
          name-value="value"
          v-model="user.enable"
        >
          <el-option
            v-for="item in $CONST.ACTIVE_STATUS"
            :key="item.name"
            :label="item.title"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button v-if="id" type="primary" @click="onSubmit(true)" v-waves
          >保存</el-button
        >
        <el-button v-if="!id" type="primary" @click="onSubmit(true)" v-waves
          >保存并继续</el-button
        >
        <el-button v-if="!id" type="primary" @click="onSubmit(false)" v-waves
          >保存并退出</el-button
        >
        <el-button v-waves @click="onCancel">取消</el-button>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<style scoped>
.el-form {
  margin: 20px;
}
</style>

<script>
import studioUserAPI from '@/api/photoStudioUser'
import waves from '@/directive/waves' // 水波纹指令
import { mapGetters } from 'vuex'
export default {
  directives: {
    waves
  },
  props: ['id'],
  data () {
    return {
      user: {
        id: 0,
        account: null,
        name: null
      },
      formRules: {
        account: [
          {
            required: true,
            message: '请输入账号'
          },
          {
            min: 5,
            max: 20,
            message: '账号长度需在5到20个字符之间'
          },
          {
            validator: (rule, value, callback) => {
              if (this.id) {
                return callback()
              }
              studioUserAPI.checkAccount(value).then((res) => {
                if (!res.data) {
                  return callback(new Error('账号已存在，请重新输入'))
                }
                return callback()
              })
            },
            trigger: 'blur'
          }
        ],
        name: [
          {
            required: true,
            message: '请输入管理员名称'
          },
          {
            max: 10,
            message: '管理员名称长度不能超过 10 个字符'
          }
        ],
        password: [
          {
            required: true,
            message: '请输入管理员密码'
          },
          {
            min: 6,
            max: 20,
            message: '密码长度在 6 到 20 个字符'
          }
        ]
      }
    }
  },
  watch: {
    id () {
      this.resetForm()
    }
  },
  computed: {
    ...mapGetters(['token']),
    isEditMode () {
      return this.id && this.id > 0
    }
  },
  methods: {
    /**
     * 提交数据
     * @param {*} goon 新增后是否继续新增
     */
    onSubmit (goon) {
      this.$refs.form.validate().then((valid) => {
        if (!valid) {
          return
        }
        if (!this.id) {
          studioUserAPI.insert(this.user).then((response) => {
            this.$notify({
              title: '成功',
              message: '创建成功',
              type: 'success'
            })
            if (goon) {
              this.resetForm()
            } else {
              this.$router.replaceBy(this.$router.routesConfig.studioUserList)
            }
          })
        } else {
          studioUserAPI.update(this.user).then((response) => {
            this.$notify({
              title: '成功',
              message: '更新成功',
              type: 'success'
            })
            this.$router.replaceBy(this.$router.routesConfig.studioUserList)
          })
        }
      })
    },
    /**
     * 取消按钮处理
     */
    onCancel () {
      this.$router.replaceBy(this.$router.routesConfig.studioUserList)
    },
    /**
     * 获取管理员信息
     */
    getUser () {
      if (this.isEditMode) {
        studioUserAPI.getById(this.id).then((res) => {
          this.user = res.data
        })
      }
    },
    /**
     * 重置表单
     */
    resetForm () {
      this.$refs.form.resetFields()
      this.user.id = ''
    }
  },
  created () {
    this.getUser()
  }
}
</script>
